package com.hss.thread;

import sun.rmi.runtime.Log;

import java.util.concurrent.*;

/**
 *
 *
 shutDown()，关闭线程池，需要执行完已提交的任务；
 shutDownNow()，关闭线程池，并尝试结束已提交的任务；
 allowCoreThreadTimeOut(boolen)，允许核心线程闲置超时回收；
 execute()，提交任务无返回值；
 submit()，提交任务有返回值；

 */
public class ThreadExecutorTest {
    public static void main(String[] args) {
//        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor();

//        newFixedThreadPool();
//        newSingleThreadExecutor();
//        newCachedThreadPool();
        newScheduledThreadPool();
    }

//创建了一个线程数为5的固定线程数量的线程池，同理该线程池支持的线程最大并发数也是5，模拟20个任务让它处理，执行任务
    public static void newFixedThreadPool(){
        ExecutorService fixedThreadPool = Executors.newFixedThreadPool(5);
        for (int i=0;i<20;i++){
            final  int index = i;
            fixedThreadPool.execute(new Runnable() {
                @Override
                public void run() {
                    String threadName = Thread.currentThread().getName();
                    System.out.println("线程："+threadName+",正在执行第" + index + "个任务");
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    }

//改了线程池的实现方式，即依次一个一个的处理任务，而且都是复用一个线程
    public static void newSingleThreadExecutor(){
        ExecutorService singleThreadPool  = Executors.newSingleThreadExecutor();
        for (int i=0;i<10;i++){
            final  int index = i;
            singleThreadPool .execute(new Runnable() {
                @Override
                public void run() {
                    String threadName = Thread.currentThread().getName();
                    System.out.println("线程："+threadName+",正在执行第" + index + "个任务");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    }

//为了体现该线程池可以自动根据实现情况进行线程的重用，而不是一味的创建新的线程去处理任务
    public static void newCachedThreadPool() {
        ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
        for (int i = 1; i <= 20; i++) {
            final int index = i;
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            cachedThreadPool.execute(new Runnable() {
                @Override
                public void run() {
                    String threadName = Thread.currentThread().getName();
                    System.out.println("线程：" + threadName + ",正在执行第" + index + "个任务");
                    try {
                        long time = index * 500;
                        Thread.sleep(time);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    }
//通过日志可以发现schedule方法的任务只是执行了一次，然后每隔2秒执行一次该scheduleAtFixedRate方法中的任务
    public static void newScheduledThreadPool(){
        ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5);
        //延迟2秒后执行该任务
        scheduledThreadPool.schedule(new Runnable() {
//            @SuppressLint("LongLogTag")
            @Override
            public void run() {
                String threadName = Thread.currentThread().getName();
                System.out.println( "线程：" + threadName + ",正在执行");
            }
        }, 2, TimeUnit.SECONDS);
        //延迟1秒后，每隔2秒执行一次该任务
        scheduledThreadPool.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                String threadName = Thread.currentThread().getName();
                System.out.println("线程：" + threadName + ",正在执行");
            }
        }, 1, 2, TimeUnit.SECONDS);
    }
}
